//package zengjin.test;
//
//import jakarta.servlet.http.HttpServletRequest;
//import org.apache.commons.fileupload.RequestContext;
//import org.apache.commons.fileupload.disk.DiskFileItemFactory;
//import org.apache.commons.fileupload.servlet.ServletFileUpload;
//import org.springframework.web.bind.annotation.*;
//import org.springframework.web.multipart.MultipartFile;
//import org.springframework.web.multipart.MultipartHttpServletRequest;
//
//import java.io.*;
//import java.nio.file.Files;
//import java.nio.file.Paths;
//import java.util.List;
//
///**
// * @author 19324
// */
//@RestController
//@RequestMapping("/upload")
//public class FileUploadController {
//
//    // 分片上传接口
//    @PostMapping("/chunk")
//    public String uploadChunk(HttpServletRequest request) throws Exception {
//        // 检查请求是否为文件上传请求
//        boolean isMultipart = ServletFileUpload.isMultipartContent((RequestContext) request);
//        if (isMultipart) {
//            // 创建文件项工厂
//            DiskFileItemFactory factory = new DiskFileItemFactory();
//            ServletFileUpload upload = new ServletFileUpload(factory);
//
//            // 解析请求，获取文件和参数
//            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
//            List<MultipartFile> files = multipartRequest.getFiles("file");
//            String filename = request.getParameter("filename");
//            int chunkIndex = Integer.parseInt(request.getParameter("chunkIndex"));
//            int chunkCount = Integer.parseInt(request.getParameter("chunkCount"));
//
//            // 保存分片文件
//            for (MultipartFile file : files) {
//                String chunkFilename = filename + ".part" + chunkIndex;
//                File chunkFile = new File("uploads/" + chunkFilename);
//                file.transferTo(chunkFile);
//            }
//        }
//        return "Chunk uploaded successfully";
//    }
//
//    // 合并分片接口
//    @PostMapping("/merge")
//    public String mergeChunks(@RequestParam("filename") String filename, @RequestParam("chunkCount") int chunkCount) throws IOException {
//        File outputFile = new File("uploads/" + filename);
//        try (FileOutputStream fos = new FileOutputStream(outputFile)) {
//            for (int i = 0; i < chunkCount; i++) {
//                String chunkFilename = filename + ".part" + i;
//                File chunkFile = new File("uploads/" + chunkFilename);
//                if (chunkFile.exists()) {
//                    // 读取分片文件并写入到输出文件
//                    byte[] buffer = Files.readAllBytes(Paths.get(chunkFile.getAbsolutePath()));
//                    fos.write(buffer);
//                    // 删除分片文件
//                    chunkFile.delete();
//                }
//            }
//        }
//        return "File merged successfully";
//    }
//}